package com.lin.demo.interceptor;

import com.lin.demo.MainApplication;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author zimingl
 * @Date 2023/3/30 23:08
 * @Description: 日志拦截器
 */
@Component
@Slf4j
public class LoginInterceptor extends HandlerInterceptorAdapter {
    private final static Logger logger= LoggerFactory.getLogger(MainApplication.class);

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        logger.info("perHandle");
        return true;
    }

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) {
        logger.info("postHandle");
    }

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) {
        String method = request.getMethod();
        String requestURI = request.getRequestURI();
        String remoteUser = request.getRemoteUser();
        StringBuffer requestURL = request.getRequestURL();
        logger.info("method : {}, requestURI : {}, remoteUser : {}, requestURL : {}", method, requestURI, remoteUser, requestURL);
        int status = response.getStatus();
        String contentType = response.getContentType();
        logger.info("afterCompletion, status ：{}, contentType : {}", status, contentType);
    }

    public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) {
        logger.info("afterConcurrentHandlingStarted");
    }
}
